package com.scau.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.scau.base.RandomUtil;
import com.scau.dto.CBECReturn;
import com.scau.exception.GoodsStockException;
import com.scau.exception.OrderItemException;
import com.scau.handleException.ResultCode;
import com.scau.pojo.*;
import com.scau.pojo.integration.PushRecordComplete;
import com.scau.pojo.integration.WishListComplete;
import com.scau.service.*;
import net.sf.ehcache.search.expression.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@CrossOrigin
@RequestMapping("")
public class SellerController {

    @Autowired
    ISystemUserService systemUserService;

    @Autowired
    IEShopService eShopService;

    @Autowired
    IWishListService wishListService;

    @Autowired
    IGoodsService goodsService;

    @Autowired
    IOrderService orderService;

    @Autowired
    IPushRecordService pushRecordService;

    @Autowired
    IBrandControllerService brandControllerService;

    /**
     * 查看借卖方信息
     *
     * @param username
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping(value = "/seller/{username}/info", method = RequestMethod.GET, produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn showSellerInforamartion(@PathVariable("username") String username) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        SystemUser user = systemUserService.selectUser(username);
        user.setPassword("****");
        //json初始化

        return CBECReturn.ok(user);
    }

    /**
     * 修改借卖方信息
     *
     * @param user
     * @return
     */
    @RequestMapping(value = "/seller/updateUser", method = RequestMethod.POST)
    @ResponseBody
    public CBECReturn updateSellerInforamartion(SystemUser user) {
        systemUserService.updateUser(user);
        return CBECReturn.ok();
    }

    /**
     * 查看借卖方店铺信息
     *
     * @param username
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping(value = "/seller/shopper/{username}/info", method = RequestMethod.GET, produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn showShop(@PathVariable("username") String username) throws JsonProcessingException {
        //获取对应的店铺信息
        List<EShop> eShopList = eShopService.selectBySeller(username);

        return CBECReturn.build(ResultCode.SUCCESS, eShopList);
    }

    /**
     * 添加店铺信息
     * error:中文乱码问题//已解决
     *
     * @param eShop
     * @return
     */
    @RequestMapping(value = "/seller/shopper/add", method = RequestMethod.POST, produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn addShop(@RequestBody EShop eShop) {
        eShop.setId(RandomUtil.getRandomString32());
//        eShop.setUsername();
        System.out.println(eShop);
        eShopService.addEShop(eShop);
        return CBECReturn.ok();
    }

    /**
     * 修改店铺信息
     * errorr：中文乱码//已解决
     *
     * @param eShop
     * @return
     */
    @RequestMapping(value = "/seller/shopper/update", method = RequestMethod.POST)
    @ResponseBody
    public CBECReturn updateShop(EShop eShop) {
        eShopService.updateEShop(eShop);
        return CBECReturn.ok();
    }

    /**
     * 按照id删除对应的店铺
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/seller/shopper/delete/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CBECReturn deleteShop(@PathVariable String id) {
        eShopService.deleteEshop(id);
        return CBECReturn.ok();
    }

    //浏览商品，按店铺分类
    @RequestMapping(value = "/user/goods/{username}", method = RequestMethod.GET)
    public String showGoods() {

        return null;
    }

    /**
     * 查看心愿单
     *
     * @param username
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping(value = "/user/wishList/{username}/info", method = RequestMethod.GET, produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn showWishList(@PathVariable String username) throws JsonProcessingException {

        //选择对应用户的心愿单列表
        List<WishListKey> wishListKeys = wishListService.selectByUserName(username);

        //创建完整的心愿单列表（包含商品的详细信息）
        List<WishListComplete> wishListCompleteList = new ArrayList<>();

        //遍历心愿单列表，并选择对应的商品详情，组成完整的心愿单并添加到列表中
        for (WishListKey wishListKey : wishListKeys) {
            WishListComplete wishListComplete = new WishListComplete();
            wishListComplete.setWishListKey(wishListKey);
            wishListComplete.setGoods(goodsService.getBrandGoods(wishListKey.getGoodId()));
            wishListCompleteList.add(wishListComplete);
        }


        return CBECReturn.ok(wishListCompleteList);
    }

    /**
     * 删除心愿单
     *
     * @param wishListKey
     * @return
     */
    @RequestMapping(value = "/user/wishList/delete", method = RequestMethod.DELETE, produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn deleteWishList(@RequestBody List<WishListKey> wishListKey) {
        System.out.println(wishListKey);
        for (int i = 0; i < wishListKey.size(); i++) {
            wishListService.deleteWishListItem(wishListKey.get(i).getUsername(), wishListKey.get(i).getGoodId());
        }
        return CBECReturn.ok();
    }

    /**
     * 添加心愿单
     *
     * @param wishListKey
     * @return
     */
    @RequestMapping(value = "/seller/addWishList", method = RequestMethod.POST, produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn addWishList(@RequestBody WishListKey wishListKey) {
        wishListService.addWishList(wishListKey);
        return CBECReturn.ok();
    }

    /**
     * 查看借卖方所有订单
     *
     * @param username
     * @return
     */
    @RequestMapping(value = "/user/{username}/order", method = RequestMethod.GET)
    @ResponseBody
    public CBECReturn showOrder(@PathVariable String username) {

        List<OrderItem> orderItems = orderService.getBuyerOrderItems(username);
        return CBECReturn.ok(orderItems);
    }


    /**
     * 按照对应的借卖方用户名来查找对应的完整推送记录
     *
     * @param username
     * @return
     */
    @RequestMapping(value = "/seller/shopper/{username}/pushRecord")
    @ResponseBody
    public CBECReturn showPushRecord(@PathVariable String username) {
        //将对应的推送数据整合到一个完整的推送数据实体中，并为其创建列表
        List<PushRecordComplete> pushRecordCompleteList = new ArrayList<>();
        List<EShop> eShopList = eShopService.selectBySeller(username);

        //为上述实体加入对应的数据
        for (EShop eShop : eShopList) {
            List<PushRecord> pushRecordList = pushRecordService.seletPushRecordByEshopId(eShop.getId());
            for (PushRecord pushRecord : pushRecordList) {
                PushRecordComplete pushRecordComplete = new PushRecordComplete();
                pushRecordComplete.setGoods(goodsService.getBrandGoods(pushRecord.getGoodId()));
                pushRecordComplete.setId(pushRecord.getId());
                pushRecordComplete.seteShop(eShop);
                pushRecordCompleteList.add(pushRecordComplete);
            }
        }
        return CBECReturn.ok(pushRecordCompleteList);
    }

    /**
     * 添加对应的推送记录
     *
     * @param eShopId
     * @param goodsId
     * @return
     */
    @RequestMapping(value = "seller/shopper/{eShopId}/{goodsId}/add")
    @ResponseBody
    public CBECReturn addPushRecord(@PathVariable String eShopId, @PathVariable String goodsId) {
        PushRecord pushRecord = new PushRecord();
        pushRecord.setId(RandomUtil.getRandomString32());
        pushRecord.setEshopId(eShopId);
        pushRecord.setGoodId(goodsId);
        pushRecordService.insertRecored(pushRecord);
        return CBECReturn.ok();
    }

    /**
     * 需要传入对应的goodId、buyerName、sellerName、goodsQty
     *
     * @param orderItem
     * @return
     */
    @RequestMapping(value = "seller/addOrder", method = RequestMethod.POST, produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn createOrder(@RequestBody OrderItem orderItem) {
        String orderId = RandomUtil.getRandomString32();
        orderItem.setId(orderId);
        orderItem.setBuyerAddr("scau");
        orderItem.setSellerAddr("scau");
        orderItem.setCreateDate(new Date());
        orderItem.setStatus(1);
//        System.out.println(orderItem);
        orderService.addOrderItem(orderItem);
        return CBECReturn.ok();
    }

    @RequestMapping(value = "seller/updateOrder", method = RequestMethod.POST, produces = "application/json;charset=UTF-8;")
    @ResponseBody
    public CBECReturn updateOrder(@RequestBody OrderItem orderItem) throws GoodsStockException, OrderItemException {
        //由于功能相同，故复用品牌商的
        brandControllerService.updateBrandOrderStatus(orderItem.getId(),orderItem.getStatus());
        return CBECReturn.ok();
    }


}
